package com.music.playlist.controllers;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.music.client.playlist.client.PlaylistClient;
import com.music.service.playlists.beans.Playlist;
import static com.music.playlist.constants.PlaylistConstants.*;

public class MyPlaylistController extends MultiActionController {

	private String mViewPlaylistView;
	private String mEditPlaylistView;
	private String mDeletePlaylistView;
	private String mExceptionView;

	private PlaylistClient mPlaylistClient;

	/**
	 * Method to view songs of a playlist
	 * @param pRequest
	 * @param pResponse
	 * @return ModelAndView
	 * @throws ServletException
	 * @throws IOException
	 */
	public ModelAndView viewPlaylist(HttpServletRequest pRequest,
			HttpServletResponse pResponse) throws ServletException, IOException {
		ModelAndView modelAndView = new ModelAndView();
		Long pMemberId = null;
		Long pPlaylistId = null;
		try {
			pMemberId = getMemberId(pRequest);
			pPlaylistId = getPlaylistId(pRequest);
		} catch (NumberFormatException nfe) {
			modelAndView.setViewName(getExceptionView());
			modelAndView.addObject(EXCEPTION_MESSAGE,
					"Invalid Member Id or Playlist Id");
			return modelAndView;
		}

		Playlist playlist = getPlaylistClient().getPlaylistSongsForMember(
				pMemberId, pPlaylistId);
		List<Playlist> recentPlaylist = getPlaylistClient().getRecentPlaylists(
				pMemberId);

		modelAndView.addObject(PLAYLIST, playlist);
		modelAndView.addObject(RECENT_PLAYLISTS, recentPlaylist);
		modelAndView.setViewName(getViewPlaylistView());

		return modelAndView;
	}

	/**
	 * Method to edit playlist properties such as playlist name,shuffle,repeat etc.
	 * @param pRequest
	 * @param pResponse
	 * @return ModelAndView
	 * @throws ServletException
	 * @throws IOException
	 */
	public ModelAndView editPlaylist(HttpServletRequest pRequest,
			HttpServletResponse pResponse) throws ServletException, IOException {
		ModelAndView modelAndView = new ModelAndView();
		Long pMemberId = null;
		Long pPlaylistId = null;
		try {
			pMemberId = getMemberId(pRequest);
			pPlaylistId = getPlaylistId(pRequest);
		} catch (NumberFormatException nfe) {
			modelAndView.setViewName(getExceptionView());
			modelAndView.addObject(EXCEPTION_MESSAGE,
					"Invalid Member Id or Playlist Id");
			return modelAndView;
		}
		//TODO: add code to edit playlist properties.
		return modelAndView;
	}

	/**
	 * Method to delete playlist 
	 * @param pRequest
	 * @param pResponse
	 * @return ModelAndView
	 * @throws ServletException
	 * @throws IOException
	 */
	public ModelAndView deletePlaylist(HttpServletRequest pRequest,
			HttpServletResponse pResponse) throws ServletException, IOException {
		ModelAndView modelAndView = new ModelAndView(getDeletePlaylistView());
		Long pMemberId = null;
		Long pPlaylistId = null;
		try {
			pMemberId = getMemberId(pRequest);
			pPlaylistId = getPlaylistId(pRequest);
		} catch (NumberFormatException nfe) {
			modelAndView.setViewName(getExceptionView());
			modelAndView.addObject(EXCEPTION_MESSAGE,	"Invalid Member Id or Playlist Id");
			return modelAndView;
		}
		boolean isDeleted = getPlaylistClient().deletePlaylist(pMemberId, pPlaylistId);
		if(!isDeleted){
			modelAndView.setViewName(getExceptionView());
			modelAndView.addObject(EXCEPTION_MESSAGE,"Unable to delete playlist");
		}
		return modelAndView;
	}

	/**
	 * 
	 * @param pRequest
	 * @return
	 */
	private Long getMemberId(HttpServletRequest pRequest){
		return Long.valueOf(pRequest.getParameter(PARAM_MEMBER_ID));
	}
	
	/**
	 * 
	 * @param pRequest
	 * @return
	 */
	private Long getPlaylistId(HttpServletRequest pRequest){
		return Long.valueOf(pRequest.getParameter(PARAM_PLAYLIST_ID));
	}
	
	
	/**
	 * @return the viewPlaylistView
	 */
	public String getViewPlaylistView() {
		return mViewPlaylistView;
	}

	/**
	 * @param pViewPlaylistView
	 *            the viewPlaylistView to set
	 */
	@Required
	public void setViewPlaylistView(String pViewPlaylistView) {
		mViewPlaylistView = pViewPlaylistView;
	}

	/**
	 * @return the editPlaylistView
	 */
	public String getEditPlaylistView() {
		return mEditPlaylistView;
	}

	/**
	 * @param pEditPlaylistView
	 *            the editPlaylistView to set
	 */
	@Required
	public void setEditPlaylistView(String pEditPlaylistView) {
		mEditPlaylistView = pEditPlaylistView;
	}

	/**
	 * @return the deletePlaylistView
	 */
	public String getDeletePlaylistView() {
		return mDeletePlaylistView;
	}

	/**
	 * @param pDeletePlaylistView
	 *            the deletePlaylistView to set
	 */
	@Required
	public void setDeletePlaylistView(String pDeletePlaylistView) {
		mDeletePlaylistView = pDeletePlaylistView;
	}

	/**
	 * @return the exceptionView
	 */
	public String getExceptionView() {
		return mExceptionView;
	}

	/**
	 * @param pExceptionView
	 *            the exceptionView to set
	 */
	@Required
	public void setExceptionView(String pExceptionView) {
		mExceptionView = pExceptionView;
	}

	/**
	 * @return the playlistClient
	 */
	public PlaylistClient getPlaylistClient() {
		return mPlaylistClient;
	}

	/**
	 * @param pPlaylistClient
	 *            the playlistClient to set
	 */
	@Required
	public void setPlaylistClient(PlaylistClient pPlaylistClient) {
		mPlaylistClient = pPlaylistClient;
	}

}
